import request from "@/utils/request";

// 将搜索表单对象转换为 GET 查询参数
const buildParams = (form = {}) => {
  const params = {};
  // 分页
  if (form.page != null) params.page = form.page;
  if (form.pageSize != null) params.pageSize = form.pageSize;
  // 时间范围
  if (Array.isArray(form.consumeRange) && form.consumeRange.length === 2) {
    const [begin, end] = form.consumeRange;
    if (begin) params.begin = begin;
    if (end) params.end = end;
  }
  // 其它筛选
  if (form.userKeyword) params.userKeyword = form.userKeyword;
  if (form.merchantType) params.merchantType = form.merchantType;
  if (form.merchantName) params.merchantName = form.merchantName;
  if (form.minAmount != null && form.minAmount !== "") params.minAmount = form.minAmount;
  if (form.maxAmount != null && form.maxAmount !== "") params.maxAmount = form.maxAmount;
  if (form.abnormalStatus && form.abnormalStatus !== "all") params.abnormalStatus = form.abnormalStatus;
  return params;
};

// 消费记录分页查询（GET，使用 params 携带查询参数）
export const queryConsumePageApi = (form) => {
  const params = buildParams(form);
  return request.get("/consumptionRecord/list", { params });
};

export const queryHeatMapApi = (params) => {
  // 仅传递非空参数
  const qp = {}
  if (params?.startTime) qp.startTime = params.startTime
  if (params?.endTime) qp.endTime = params.endTime
  if (params?.merchantType) qp.merchantType = params.merchantType
  return request.get('/consumptionRecord/heatMap', { params: qp })
}
